package net.flyinggroup.mapstruct.mapper;

import net.flyinggroup.mapstruct.model.BioDieselCar;
import net.flyinggroup.mapstruct.model.Car;
import net.flyinggroup.mapstruct.model.CarDTO;
import net.flyinggroup.mapstruct.model.ElectricCar;
import org.mapstruct.*;

@Mapper
public abstract class CarsMapper {
    @BeforeMapping
    protected void enrichDTOWithFuelType(Car car, @MappingTarget CarDTO carDto) {
        if (car instanceof ElectricCar) {
            carDto.setFuelType(CarDTO.FuelType.ELECTRIC);
        }
        if (car instanceof BioDieselCar) {
            carDto.setFuelType(CarDTO.FuelType.BIO_DIESEL);
        }
    }

    @AfterMapping
    protected void convertNameToUppercase(@MappingTarget CarDTO carDto) {
        carDto.setName(carDto.getName().toUpperCase());
    }

    @Mapping(target = "fuelType", ignore = true)
    public abstract CarDTO toCarDTO(Car car);
}
